<template>
	<page-body>
		<view class="page">
			<view class="flex benben-position-layout flex flex-wrap align-center talk_flex_0"
				:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
				<view class='flex flex-wrap align-center justify-between flex-sub talk_fd0_0'>
					<view class='flex flex-wrap align-center talk_fd0_0_c0'>
						<text class='fu-iconfont2  talk_fd0_0_c0_c0' @tap.stop="handleJumpDiy" data-type="back"
							data-url="1">&#xE794;</text>
					</view>
					<view class='flex flex-wrap align-stretch justify-center'>
						<text class='talk_fd0_0_c1_c0'>举报</text>
					</view>
					<view class='flex flex-wrap align-center justify-end talk_fd0_0_c0'>
					</view>
				</view>
			</view>
			<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
			<view class="page-content">
				<view class="type">
					请选择你想要举报的类型<text style="color: #D32A29;">*</text>
				</view>
				<view class="type-outer">
					<view class="type-ite" :class="submitObj.type == ite.aid ? 'type-ite-other' : ''"
						v-for="ite in typeList" :key="ite.aid" @click="submitObj.type = ite.aid">
						{{ite.name}}
					</view>
				</view>
				<view class="dect">
					举报描述（选填）<text
						style="font-size: 32rpx;color: #999999;float: right;">{{submitObj.descript.length}}/200</text>
				</view>
				<view style="margin: 0 24rpx;" class="">
					<uni-easyinput type="textarea" :maxlength="200" v-model="submitObj.descript"
						placeholder="请输入内容"></uni-easyinput>
				</view>
				<view class="dect">
					图片证据（选填）<text style="font-size: 32rpx;color: #999999;float: right;">最多三张</text>
				</view>

				<view style="margin: 0 24rpx;" class="">
					<uni-file-picker v-model="fileList" @delete="deleteFile" :limit="3" file-mediatype="image"
						:disable-preview="true" @select="selectFile">
						<view class="custom-add">
							<image style="width: 24rpx;height: 24rpx;margin-bottom: 16rpx;"
								src="@/static/talkImage/add.png" mode=""></image>
							<text>上传图片</text>
						</view>
					</uni-file-picker>
				</view>
			</view>

			<view class="flex benben-position-layout flex flex-wrap align-center coursePro_flex_3"
				:style="{height:(150+bottomSafeAreaRpx)+'rpx',paddingBottom:bottomSafeAreaRpx+'rpx', }">
				<button class='coursePro_fd3_0' @tap.stop="submit()">提交</button>
			</view>
			<view :style="{height: (150+bottomSafeAreaRpx)+'rpx'}"></view>
		</view>
	</page-body>
</template>
<script>
	import {
		validate
	} from '@/common/utils/validate.js'
	import {
		host,
		SERIAL,
		API_VERSION,
		SERVER_TYPE
	} from '@/common/config.js'
	import {
		uniEasyinput,
		uniFilePicker
	} from '@dcloudio/uni-ui'

	export default {
		data() {
			return {
				groupId: "",
				typeList: ['广告内容', '搬运抄袭', '不友善内容', '造谣、伪科学', '虚假互动数据', '色情低俗', '违法违规', '侵权投诉', '青少年不宜', '其他', ],
				textValue: "",
				fileList: [],
				submitObj: {
					type: '',
					descript: "",
					imgList: []
				}
			}
		},
		components: {
			uniEasyinput,
			uniFilePicker
		},
		// 小程序页面生命周期，如果是 uni-app 可以写
		onLoad(options) {
			this.groupId = decodeURIComponent(options.groupId)

			this.get_reporttype()
		},
		computed: {
			// 计算属性
		},
		watch: {
			// 监听器
		},
		methods: {
			deleteFile(e) {
				this.submitObj.imgList.splice(e.index, 1)
			},
			async get_reporttype() {
				let res = await this.$api.get(global.apiUrls.post68c22c7f677e3, {})

				if (res.data.code == 1) {
					this.typeList = res.data.data
				}
			},
			// 选择文件时触发
			selectFile(e) {
				console.log('选择的文件：', e.tempFiles)
				e.tempFiles.forEach(file => {
					// 调用自定义上传方法
					this.uploadToServer(file)
				})
			},
			// 上传到自己服务器
			uploadToServer(file) {
				console.log('11111111111111111111', file);
				uni.uploadFile({
					url: this.posUrl(global.apiUrls.UPLOAD_IMAGE_URL2),
					filePath: file.path, // 临时文件路径
					name: 'file', // 后端接收的字段名
					fileType: "image",
					header: {
						'user-token': global.token || '',
						'Accept-Language': global.locale,
					},
					formData: {
						'dir_path': '/reportPage'
					},
					success: res => {
						const res2 = JSON.parse(res.data)

						this.submitObj.imgList.push(res2.data.file)

					},
					fail: err => {
						console.error('上传失败:', err)
					}
				})
			},
			/* 判断url路径处理 */
			posUrl(url) {
				let newUrl = ''
				if (/(http|https):\/\/([\w.]+\/?)\S*/.test(url)) {
					newUrl = url
				} else {
					newUrl = `${host}/api/${url}`
				}
				return newUrl
			},
			async submit() {
				console.log('1111111111111', this.submitObj);
				if (!this.submitObj.type) {
					uni.showToast({
						title: '请选择要举报的类型',
						icon: 'none'
					})
					return
				}

				let res = await this.$api.post(global.apiUrls.post68c22e47f0304, {
					refer: this.submitObj.descript,
					thumb: this.submitObj.imgList.join(','),
					reporttype_id: this.submitObj.type,
					imgroup_id: this.groupId,
				})

				if (res.data.code == 1) {
					uni.showToast({
						title: '操作成功',
						icon: 'none'
					})

					setTimeout(() => {
						uni.navigateBack({
							delta: 1 // 返回的层数，1 表示返回上一级，2 表示返回上两级
						})
					}, 1000)

				}

			}
		},

		onReady() {},
		onShow() {},
		onHide() {},
		onUnload() {},
	}
</script>
<style lang="scss" scoped>
	.coursePro_flex_3 {
		width: 750rpx;
		overflow: hidden;
		z-index: 10;
		bottom: calc(0rpx + var(--window-bottom));
		background-color: #fff;
	}

	.coursePro_fd3_0 {
		width: 702rpx;
		height: 84rpx;
		background: #145EFD;
		line-height: 84rpx;
		font-size: 30rpx;
		color: #fff;
		border-radius: 12rpx;
		margin: 0 auto
	}

	::v-deep .file-picker__box-content {
		width: 160rpx;
		height: 160rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		border: 1rpx solid #CCCCCC;
	}

	.custom-add {
		width: 160rpx;
		height: 160rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-weight: 400;
		font-size: 24rpx;
		color: #999999;
	}

	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #fff;
		background-size: 100% auto;
	}

	.page-content {
		.type {
			font-weight: 400;
			font-size: 32rpx;
			color: #333333;
			line-height: 46rpx;
			margin: 24rpx;
		}

		.dect {
			font-weight: 400;
			font-size: 32rpx;
			color: #333333;
			line-height: 46rpx;
			margin: 20rpx 24rpx 24rpx;
		}

		.type-outer {
			margin: 0 24rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.type-ite {
				width: 339rpx;
				height: 82rpx;
				background: #FFFFFF;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				border: 1rpx solid #CCCCCC;
				font-weight: 400;
				font-size: 32rpx;
				color: #666666;
				line-height: 82rpx;
				text-align: center;
				margin-bottom: 24rpx;
			}

			.type-ite-other {
				background: #145EFD;
				border: 1px solid #145EFD;
				color: #fff;
			}
		}
	}

	.talk_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.talk_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 700;
		color: #333333;
		line-height: 50rpx;
	}

	.talk_fd0_0_c0_c0 {
		font-size: 36rpx;
		color: #333;
	}

	.talk_fd0_0_c0 {
		width: 120rpx;
	}

	.talk_fd0_0 {
		padding: 0rpx 32rpx 0rpx 32rpx;
	}

	.talk_flex_1 {
		padding: 24rpx;
	}
</style>